# encoding: utf-8
'''
Created on 2018-8-13
desription:日志助手类
@author: zhujianhua
'''
from logging.handlers import TimedRotatingFileHandler
import logging
import sys
import os

sys.path.append('./helper')
import InfoMonConstants

class Singleton(object):
    loginstance = {}
    def __new__(cls, loggerName):
        if not cls.loginstance.has_key(loggerName):
            cls.loginstance.update({loggerName: object.__new__(cls)})
            return cls.loginstance[loggerName]
        return cls.loginstance[loggerName]

class LoggerHelper(Singleton,object):
    logger = None
    def __init__(self,loggerName):
        if (self.logger == None):
            self.logger = logging.getLogger(loggerName)
            self.logger.setLevel(int(InfoMonConstants.LOG_LEVEL))
            
            logDir = os.path.join(os.getcwd(), 'log')
            if not os.path.exists(logDir):
                os.mkdir(logDir)
            
            loggerHandler = TimedRotatingFileHandler(os.path.join(logDir, InfoMonConstants.LOG_FILE), when="D", backupCount=3)
            formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
            loggerHandler.setFormatter(formatter)
            
            self.logger.addHandler(loggerHandler)
    
    def debug(self,msg):
        self.logger.debug(msg)
        
    def info(self,msg):
        self.logger.info(msg)
        
    def warning(self,msg):
        self.logger.warning(msg)
        
    def error(self,msg):
        self.logger.error(msg)
        
    def critical(self,msg):
        self.logger.critical(msg)
        